/////////////////////////////////////////////////////////////////////////////
// Name:        ax_menu_visual_settings.cpp
// Purpose:     Visual settings menu class
// Author:      Julian Smart
// Modified by:
// Created:     2009-03-20
// RCS-ID:
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"
#include "wx/spinctrl.h"

#include "ax/ax_menu_alert.h"
#include "ax/ax_menu_visual_settings.h"
#include "ax/ax_speech.h"
#include "ax/ax_menu_toggle.h"
#include "ax/ax_menu_range.h"
#include "ax/ax_menu_choice.h"
#include "ax/ax_menu_folder.h"
#include "ax/ax_view.h"
#include "ax/ax_menu_colour.h"

/**
    @class AxVisualSettingsTextSizeValidator
 */

class AxVisualSettingsTextSizeValidator: public AxValidator
{
    DECLARE_DYNAMIC_CLASS( AxVisualSettingsTextSizeValidator )

public:
// Constructors

    AxVisualSettingsTextSizeValidator(const AxVisualSettingsTextSizeValidator& validator) { Init(); Copy(validator); }
    AxVisualSettingsTextSizeValidator() { Init(); }

// Operations

    void Copy(const AxVisualSettingsTextSizeValidator& validator)
    {
        AxValidator::Copy(validator);
    }

    virtual AxValidator* Clone() const { return new AxVisualSettingsTextSizeValidator(*this); }

    /// Override to pass a value from the UI to a data structure
    virtual bool TransferDataFromMenu();

    /// Override to pass a value from a data structure to the UI
    virtual bool TransferDataToMenu();

    /// Initialize variables
    void Init() { }

protected:
};

/*
 * AxVisualSettingsMenuItem implementation.
 */

IMPLEMENT_CLASS( AxVisualSettingsMenuItem, AxMenuItem )

BEGIN_EVENT_TABLE( AxVisualSettingsMenuItem, AxMenuItem )
    EVT_CHOICE(AXID("TEXTSIZE"), AxVisualSettingsMenuItem::OnSelectTextSize)
    EVT_CHOICE(AXID("TEXTCOLOUR"), AxVisualSettingsMenuItem::OnSelectTextColour)
    EVT_CHOICE(AXID("WINDOWCOLOUR"), AxVisualSettingsMenuItem::OnSelectWindowColour)
    EVT_CHOICE(AXID("PANELCOLOUR"), AxVisualSettingsMenuItem::OnSelectPanelColour)
END_EVENT_TABLE()

AxVisualSettingsMenuItem::AxVisualSettingsMenuItem(AxMenuController* controller, AxSpeech* speech)
{
    m_speech = speech;
    m_controller = controller;

    SetLabel(_("Visual Settings"));
    SetDescription(_("Lets you change the appearance of the program."));

    SetDynamic();

    AxVisualSettingsActivator* activator = new AxVisualSettingsActivator(speech);
    activator->SetMenuItem(this);
    SetActivator(activator);
}

void AxVisualSettingsMenuItem::OnSelectTextSize(wxCommandEvent& WXUNUSED(event))
{
    if (AxViewManager::Get())
        AxViewManager::Get()->UpdateAppearance(NULL, AxVisualSettingTextSize);
}

void AxVisualSettingsMenuItem::OnSelectTextColour(wxCommandEvent& WXUNUSED(event))
{
    if (AxViewManager::Get())
        AxViewManager::Get()->UpdateAppearance(NULL, AxVisualSettingTextColour);
}

void AxVisualSettingsMenuItem::OnSelectWindowColour(wxCommandEvent& WXUNUSED(event))
{
    if (AxViewManager::Get())
        AxViewManager::Get()->UpdateAppearance(NULL, AxVisualSettingWindowColour);
}

void AxVisualSettingsMenuItem::OnSelectPanelColour(wxCommandEvent& WXUNUSED(event))
{
    if (AxViewManager::Get())
        AxViewManager::Get()->UpdateAppearance(NULL, AxVisualSettingPanelColour);
}

IMPLEMENT_DYNAMIC_CLASS( AxVisualSettingsActivator,  AxActivator )

/// Called when the user activates a menu item by pressing return or double-clicking
AxMenu* AxVisualSettingsActivator::Activate(AxMenuController* controller, AxMenuItem* WXUNUSED(it))
{
    AxMenu* menu = new AxMenu;
    menu->SetLabel(_("Visual Settings"));
    menu->SetDescription(_("Lets you change the appearance of the program."));

    //// TEXT SIZE

    wxArrayString textSizes;
    wxArrayString textSizesDescriptions;
    wxArrayString textSizesIds;
    {
        textSizes.Add(_("System default"));
        textSizesDescriptions.Add(_("Use the system default text size."));
        textSizesIds.Add(wxT("SYSTEMDEFAULT"));
    }
    int i;
    for (i = 8; i < 20; i++)
    {
        wxString strSize = wxString::Format(wxT("%d"), i);
        wxString strDescr = wxString::Format(_("Selects a text size of %d points."), i);
        wxString strId = strSize;

        textSizes.Add(strSize);
        textSizesDescriptions.Add(strDescr);
        textSizesIds.Add(strId);
    }
    int currentTextSize = controller->GetViewManager()->GetVisualSettings().GetTextSize();
    int textIdx = 0;
    if (currentTextSize == -1)
        textIdx = 0;
    else
    {
        int pos = textSizes.Index(wxString::Format(wxT("%d"), currentTextSize));
        if (pos == -1)
            textIdx = 0;
        else
            textIdx = pos;
    }
    
    AxChoiceMenuItem* textSizeItem = new AxChoiceMenuItem(_("Text size: %VALUE%"),
        _("Choose a text size."),
        wxT("TEXTSIZE"), textSizes, textSizesIds, textSizesDescriptions,
        textIdx);
    textSizeItem->SetValidator(new AxVisualSettingsTextSizeValidator);
    menu->AddItem(textSizeItem);

    //// Text colour menu

    AxColourMenuItem* textColourItem = new AxColourMenuItem(_("Text colour: %VALUE%"),
        _("Change the colour of text in controls."),
        _("TEXTCOLOUR"),
        controller->GetViewManager()->GetVisualSettings().GetTextColour());
    textColourItem->SetValidator(new AxColourValidator(& controller->GetViewManager()->GetVisualSettings().m_textColour));
    menu->AddItem(textColourItem);

    //// Window colour menu

    AxColourMenuItem* windowColourItem = new AxColourMenuItem(_("Window colour: %VALUE%"),
        _("Change the colour of windows."),
        _("WINDOWCOLOUR"),
        controller->GetViewManager()->GetVisualSettings().GetWindowColour());
    windowColourItem->SetValidator(new AxColourValidator(& controller->GetViewManager()->GetVisualSettings().m_windowColour));
    menu->AddItem(windowColourItem);

    //// Panel colour menu

    AxColourMenuItem* panelColourItem = new AxColourMenuItem(_("Panel colour: %VALUE%"),
        _("Change the colour of panels."),
        _("PANELCOLOUR"),
        controller->GetViewManager()->GetVisualSettings().GetPanelColour());
    panelColourItem->SetValidator(new AxColourValidator(& controller->GetViewManager()->GetVisualSettings().m_panelColour));
    menu->AddItem(panelColourItem);

    menu->AddItem(new AxGoUpMenuItem);

    return menu;
}

IMPLEMENT_DYNAMIC_CLASS( AxVisualSettingsTextSizeValidator, AxValidator )

/// Override to pass a value from the UI to a data structure
bool AxVisualSettingsTextSizeValidator::TransferDataFromMenu()
{
    AxChoiceMenuItem* choiceMenuItem = wxDynamicCast(GetMenuItem(), AxChoiceMenuItem);
    if (choiceMenuItem && AxViewManager::Get())
    {
        //int oldValue = AxViewManager::Get()->GetVisualSettings().GetTextSize();
        int newValue = 0;

        wxString value = choiceMenuItem->GetSelectionId();
        if (value == wxT("SYSTEMDEFAULT"))
            newValue = -1;
        else
        {
            int sz = wxAtoi(value);
            if (sz == 0)
                sz = -1;
            newValue = sz;
        }
        AxViewManager::Get()->GetVisualSettings().SetTextSize(newValue);
    }

    return true;
}

/// Override to pass a value from a data structure to the UI
bool AxVisualSettingsTextSizeValidator::TransferDataToMenu()
{
    AxChoiceMenuItem* choiceMenuItem = wxDynamicCast(GetMenuItem(), AxChoiceMenuItem);
    if (choiceMenuItem && AxViewManager::Get())
    {
        wxString value;
        if (AxViewManager::Get()->GetVisualSettings().GetTextSize() == -1)
            value = wxT("SYSTEMDEFAULT");
        else
            value = wxString::Format(wxT("%d"), AxViewManager::Get()->GetVisualSettings().GetTextSize());

        choiceMenuItem->SetSelectionById(value);
    }

    return true;
}
